//
//  AppDelegate.swift
//  MyWeibo
//
//  Created by 余丽丽 on 15/10/7.
//  Copyright © 2015年 余丽丽. All rights reserved.
//

import UIKit

/// 在类的外面写的常量或者变量就是全局能够访问的
/// 视图控制器切换通知字符串
let YLLRootViewControllerSwitchNotification = "YLLRootViewControllerSwitchNotification"

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?


    func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
        
        //注册通知 登录成功就可以发送这个消息 object:false 进入欢迎界面;版本更新发送消息 object:true 进入新特性界面
        //这里的执行指定的方法别忘了加:
        NSNotificationCenter.defaultCenter().addObserver(self, selector: "switchViewController:", name: "YLLRootViewControllerSwitchNotification", object: nil)
        
        //设置全局外观
        setupAppearance()
        
        //1.创建窗口
        window = UIWindow(frame: UIScreen.mainScreen().bounds)
        window?.backgroundColor = UIColor.whiteColor()
        
        //2.创建控制器设置为窗口的跟控制器
        window?.rootViewController = defaultViewController()
        
        //3.设置并显示主窗口
        window?.makeKeyAndVisible()
        
        print((NSSearchPathForDirectoriesInDomains(NSSearchPathDirectory.DocumentDirectory, NSSearchPathDomainMask.UserDomainMask, true).last! as NSString).stringByAppendingPathComponent("account.plist"))
        return true
    }
    
    ///  设置全局外观
    private func setupAppearance(){
    
        //设置导航栏的外观
        UINavigationBar.appearance().tintColor = UIColor.orangeColor()
        //设置tabBar的外观
        UITabBar.appearance().tintColor = UIColor.orangeColor()
    
    }
    
    // 切换控制器,记住一定要在appdelegate中统一修改
    func switchViewController(notification: NSNotification){
    
        let mainVc = notification.object as! Bool
        window?.rootViewController = mainVc ? MainTabBarViewController() : WelcomeViewController()
        
    }
    
    // 返回默认控制器
    private func defaultViewController() -> UIViewController{
    
        //判断用户是否存在,不存在进入主界面
        if !UserAccount.userLogon{
            return MainTabBarViewController()
        }

        //判断是否是新版本,如果是返回新特征,否则,返回欢迎界面
        return isNewUpdate() ? NewFeatureCollectionViewController() : WelcomeViewController()
        
    }
    
    /// 检查新版本
    private func isNewUpdate() -> Bool{
    
        //获取程序的当前版本

        let currentVersion = Double(NSBundle.mainBundle().infoDictionary!["CFBundleShortVersionString"] as! String)!
        
        //获取程序的之前版本(沙盒版本),偏好设置
        let sandboxVersion = NSUserDefaults.standardUserDefaults().doubleForKey("sandboxVersionKey")
        
        //将当前版本写入偏好设置
        NSUserDefaults.standardUserDefaults().setDouble(currentVersion, forKey: "sandboxVersionKey")
        // iOS 7.0 之后，就不需要同步了，iOS 6.0 之前，如果不同步不会第一时间写入沙盒
        NSUserDefaults.standardUserDefaults().synchronize()
        //返回比较结果
        return currentVersion > sandboxVersion
    }
    func applicationWillResignActive(application: UIApplication) {
        // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
        // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
    }

    func applicationDidEnterBackground(application: UIApplication) {
        // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
        // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
    }

    func applicationWillEnterForeground(application: UIApplication) {
        // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
    }

    func applicationDidBecomeActive(application: UIApplication) {
        // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
    }

    func applicationWillTerminate(application: UIApplication) {
        // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
    }


}

